package org.misty.seckill.config;

import org.misty.seckill.redis.RedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@Order(1)
public class RedisConfigurer {
  @Autowired
  private RedisConfig config;

  @Bean
//  @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) // 默认Singleton
  public JedisPool getPool() {
    var poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(config.getPoolMaxTotal());
    poolConfig.setMaxIdle(config.getPoolMaxIdle());
    poolConfig.setMaxWaitMillis(config.getPoolMaxWait());
    return new JedisPool(poolConfig, config.getHost(), config.getPort(), config.getTimeout());
  }
}
